﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('WmsMaterial:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="物料名称">
                <a-input v-model="queryParam.materialName" allow-clear placeholder="请输入物料名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="物料类别">
                <a-select :allowClear="true" style="width: 100%" v-model="queryParam.materialType" placeholder="请选择物料类别">
                  <a-select-option v-for="(item,index) in materialTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="物料尺寸">
                  <a-input v-model="queryParam.materialSpec" allow-clear placeholder="请输入物料尺寸"/>
                </a-form-item>
              </a-col>
              <!-- <a-col :md="8" :sm="24">
                <a-form-item label="物料密度">
                  <a-input v-model="queryParam.materialDensity" allow-clear placeholder="请输入物料密度"/>
                </a-form-item>
              </a-col>             -->
            </template>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px"> {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <a-spin :spinning="loading">
        <!--  :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" -->
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :scroll="{x: true}"
        :rowKey="(record) => record.id"
        >
        <template class="table-operator" slot="operator" v-if="hasPerm('WmsMaterial:add')" >
          <a-button type="primary" v-if="hasPerm('WmsMaterial:add')" icon="plus" @click="$refs.addForm.add()">新增</a-button>
        </template>
        <template class="table-operator" slot="operator"  >
          
          <!-- <span>
            <a-popconfirm v-if="hasPerm('WmsMaterial:tongbu')" placement="topRight" title="确认同步？" @confirm="() => handleSubmit()">
            <a class="tongbubut">同步Desk数据</a>
          </a-popconfirm>
          </span> -->
          <!-- <a-button class="tongbubut" @click="handleSubmit">同步Desk数据</a-button> -->
        </template>

        
        <span slot="inspectionMethodscopedSlots" slot-scope="text">
          {{ 'material_inspection' | dictType(text) }}
        </span>


        <span slot="materialTypescopedSlots" slot-scope="text">
          {{ 'material_type' | dictType(text) }}
        </span>


        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('WmsMaterial:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('WmsMaterial:edit') & hasPerm('WmsMaterial:delete')"/>
          <a-popconfirm v-if="hasPerm('WmsMaterial:delete')" placement="topRight" title="确认删除？" @confirm="() => WmsMaterialDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-spin>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { WmsMaterialPage, WmsMaterialDelete,Getdeskdata } from '@/api/modular/main/WmsMaterialManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm
    },
    data () {
      return {
        loading:false,
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {},
        columns: [
        {
            title: '物料编号',
            align: 'center',
sorter: true,
            dataIndex: 'materialNo',
            width:"100px",
            ellipsis:true
          },
          {
            title: '物料名称',
            align: 'center',
            dataIndex: 'materialName',
            width:"100px",
            ellipsis:true
          },
          {
            title: '物料批次',
            align: 'center',
sorter: true,
            dataIndex: 'materialBatch',
            width:"100px",
            ellipsis:true
          },
          {
            title: '物料类别',
            align: 'center',
            dataIndex: 'materialType',
            scopedSlots: { customRender: 'materialTypescopedSlots' },
            width:"100px"
          },
          {
            title: '物料尺寸',
            align: 'center',
            dataIndex: 'materialSpec',
            width:"100px"
          },
          {
            title: '物料密度',
            align: 'center',
            dataIndex: 'materialDensity',
            width:"100px"
          },
          // {
          //   title: '库区参数',
          //   align: 'center',
          //   dataIndex: 'areaNameParameter',
          //   width:"100px"
          //   // scopedSlots: { customRender: 'areaNameParameterscopedSlots' }
          // },
          // {
          //   title: '工作区参数',
          //   align: 'center',
          //   dataIndex: 'stationNameParameter',
          //   width:"120px"
          //   // scopedSlots: { customRender: 'stationNameParameterscopedSlots' }
          // },  
          {
            title: '物料检验',
            align: 'center',
            dataIndex: 'inspectionMethod',
            scopedSlots: { customRender: 'inspectionMethodscopedSlots' },
            width:"100px"
          },
          {
            title: '物料单位',
            align: 'center',
sorter: true,
            dataIndex: 'materialUnit',
            width:"100px"
          },
//           {
//             title: '库区Ids',
//             align: 'center',
// sorter: true,
//             dataIndex: 'areaIds'
//           },
//           {
//             title: '工作区Ids',
//             align: 'center',
// sorter: true,
//             dataIndex: 'stationIds'
//           },
//           {
//             title: '工段',
//             align: 'center',
// sorter: true,
//             dataIndex: 'lesWorkshopSection',
//             width:"100px"
//           },
          {
            title: '描述',
            align: 'center',
sorter: true,
            dataIndex: 'description',
            width:"100px"
          },
          {
            title: '安全存量',
            align: 'center',
sorter: true,
            dataIndex: 'safeqty',
            width:"100px"
          },
          {
            title: '最大存量',
            align: 'center',
sorter: true,
            dataIndex: 'maxImumqty',
            width:"100px"
          },
          {
            title: '最小库龄',
            align: 'center',
sorter: true,
            dataIndex: 'minstorageAge',
            width:"100px"
          },
          {
            title: '最大库龄',
            align: 'center',
sorter: true,
            dataIndex: 'maxstorageAge',
            width:"100px"
          },
//           {
//             title: '签核状态',
//             align: 'center',
// sorter: true,
//             dataIndex: 'issueState'
//           }

          // {
          //   title: '熟化时间',
          //   align: 'center',
          //   dataIndex: 'maturationTime'
          // },
          // {
          //   title: '是否需要熟化',
          //   align: 'center',
          //   customRender: (value) => (value ? <a-tag color="#87d068">是</a-tag> : <a-tag color="gray">否</a-tag>),
          //   dataIndex: 'isMaturation'
          // }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return WmsMaterialPage(Object.assign(parameter, this.queryParam)).then((res) => {
            res.data.rows = res.data.rows.map((_obj)=>{
            _obj.materialType= _obj.materialType.toString()
				    return _obj;
			    })
            return res.data
          })
        },
        inspectionMethodData: [],
        materialTypeData: [],
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {
      if (this.hasPerm('WmsMaterial:edit') || this.hasPerm('WmsMaterial:delete')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        })
      }
      const materialTypeOption = this.$options
      this.materialTypeData = materialTypeOption.filters['dictData']('material_type')
      const inspectionMethodOption = this.$options
      this.inspectionMethodData = inspectionMethodOption.filters['dictData']('material_inspection')
    },
    methods: {
      /**
       * 查询参数组装
       */
      switchingDate () {
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        return obj
      },
      WmsMaterialDelete (record) {
        WmsMaterialDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      handleSubmit() {
        this.loading = true;
        Getdeskdata().then((res) => {
          console.log('res',res)
          if (res.success) {
            this.$message.success('同步成功')
            this.$refs.table.refresh()
            this.loading = false;
          } else {
            this.$message.error('同步失败：' + res.message)
            this.loading = false;
          }
        }).catch((err) => {
          this.$message.error('同步错误：' + err.message)
          this.loading = false;
        })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;    
  }
  .tongbubut{
    border-radius: 2px;
    padding: 6px 10px 8px 10px;
    background-color: dodgerblue;color: #fff;
  }
  .tongbubut:hover{
    opacity: 0.8;
    background-color: dodgerblue;color: #fff;
  }
  button {
    margin-right: 8px;
  }
</style>
